package cn.mst.vrs.controller.admin.pay;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.mst.vrs.controller.admin.pay.vo.PayPageReqVO;
import cn.mst.vrs.controller.admin.pay.vo.PayRespVO;
import cn.mst.vrs.controller.admin.pay.vo.PaySaveReqVO;
import cn.mst.vrs.dal.dataobject.pay.PayDO;
import cn.mst.vrs.service.pay.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 支付单")
@RestController
@RequestMapping("/vrs/pay")
@Validated
public class PayController {

    @Resource
    private PayService payService;

    @PostMapping("/create")
    @Operation(summary = "创建支付单")
    @PreAuthorize("@ss.hasPermission('vrs:pay:create')")
    public CommonResult<Long> createPay(@Valid @RequestBody PaySaveReqVO createReqVO) {
        return success(payService.createPay(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新支付单")
    @PreAuthorize("@ss.hasPermission('vrs:pay:update')")
    public CommonResult<Boolean> updatePay(@Valid @RequestBody PaySaveReqVO updateReqVO) {
        payService.updatePay(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除支付单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vrs:pay:delete')")
    public CommonResult<Boolean> deletePay(@RequestParam("id") Long id) {
        payService.deletePay(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得支付单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vrs:pay:query')")
    public CommonResult<PayRespVO> getPay(@RequestParam("id") Long id) {
        PayDO pay = payService.getPay(id);
        return success(BeanUtils.toBean(pay, PayRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得支付单分页")
    @PreAuthorize("@ss.hasPermission('vrs:pay:query')")
    public CommonResult<PageResult<PayRespVO>> getPayPage(@Valid PayPageReqVO pageReqVO) {
        PageResult<PayDO> pageResult = payService.getPayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出支付单 Excel")
    @PreAuthorize("@ss.hasPermission('vrs:pay:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPayExcel(@Valid PayPageReqVO pageReqVO,
                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayDO> list = payService.getPayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "支付单.xls", "数据", PayRespVO.class,
                BeanUtils.toBean(list, PayRespVO.class));
    }

}